package com.admin.action.login_verify_action;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;

@WebServlet(value = "/textBufferedImage")
public class TextBufferedImage extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public TextBufferedImage() {
        super();
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client

     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setHeader("Cache-Control", "no-cache");
        HttpSession session = request.getSession();
        String randomString = getRandom(4);
        System.out.println(randomString);
        session.setAttribute("code", randomString);
        OutputStream os = response.getOutputStream();
        JPEGImageEncoder encode = JPEGCodec.createJPEGEncoder(os);
        BufferedImage bi = this.CreateBufferedImage(randomString);
        encode.encode(bi);
    }

    //���Ʋ����ͼƬ
    private BufferedImage CreateBufferedImage(String randomPassword) {
        int width = 100;
        int height = 40;
        BufferedImage bi = new BufferedImage (width,height,BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.clearRect(0, 0, width, height);
        g.setColor(getRandColor(200,250));
        g.fillRect(0, 0, width, height);

        Random r = new Random();
        g.setColor(getRandColor(160,200));
        g.setFont(getFont());
        for(int i = 0;i < 155;i++) {
            int x = r.nextInt(width);
            int y = r.nextInt(height);
            int x1 = r.nextInt(12);
            int y1 = r.nextInt(12);
            g.drawLine(x, y, x+x1, y+y1);
        }

        for(int i = 0;i < 4;i++) {
            String rand = randomPassword.substring(i,i+1);
            g.setColor(new Color(20+r.nextInt(110),20+r.nextInt(110),20+r.nextInt(110)));
            g.drawString(rand, 21*i+12, 30);
        }
        g.dispose();
        return bi;

    }

    //��������ɫ
    private Color getRandColor(int fc, int bc) {
        Random random = new Random();
        if(fc > 255) {
            fc = 255;
        }
        if(bc > 255) {
            bc = 255;
        }

        int r = fc + random.nextInt(bc-fc);
        int g = fc + random.nextInt(bc-fc);
        int b = fc + random.nextInt(bc-fc);
        return new Color(r,g,b);
    }

    //����������
    private Font getFont() {
        Random random = new Random();
        Font font[] = new Font[5];
        font[0] = new Font("Ravie", Font.PLAIN, 25);
        font[1] = new Font("Antique Olive Compact", Font.PLAIN, 25);
        font[2] = new Font("Forte", Font.PLAIN, 25);
        font[3] = new Font("Wide Latin", Font.PLAIN, 25);
        font[4] = new Font("Gill Sans Ultra Bold", Font.PLAIN, 23);
        return font[random.nextInt(5)];
    }

    //����length������ַ�
    public String getRandom(int length) {
        String randomCode = "";
        for (int i = 0; i < length; i++) {
            randomCode += getRandChar();
        }
        return randomCode;
    }

    //�������ַ�
    private String getRandChar() {
        int rand = (int) Math.round(Math.random() * 2);
        long itmp = 0;
        char ctmp = '\u0000';
        switch (rand) {
            case 1:
                itmp = Math.round(Math.random() * 25 + 65);
                ctmp = (char) itmp;
                return String.valueOf(ctmp);
            case 2:
                itmp = Math.round(Math.random() * 25 + 97);
                ctmp = (char) itmp;
                return String.valueOf(ctmp);
            default:
                itmp = Math.round(Math.random() * 9);
                return String.valueOf(itmp);

        }
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to
     * post.
     *
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the POST method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }
}
